package com.yaps.petstore.managedbean;

import com.yaps.petstore.dao.CategoryDAO;

import com.yaps.petstore.entity.customer.Address;
import com.yaps.petstore.entity.catalog.Category;
import com.yaps.petstore.entity.customer.Customer;
import com.yaps.petstore.service.AccountService;
import java.util.regex.Pattern;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;



@Component("account")
@Scope("session")
public class AccountController extends Controller {

    // ======================================
    // =             Attributs              = 
    // ======================================
    @Autowired
    private AccountService accountService;
    
    

    private final String cname = this.getClass().getName();

    private String login;
    private String password;
    private String password2;
    private Customer customer = new Customer();
    private Address homeAddress = new Address();
    private Boolean isLogged;
    // ======================================
    // =             Constantes             =
    // ======================================

    public Boolean getIsLogged() {
        return isLogged;
    }

    public void setIsLogged(Boolean isLogged) {
        this.isLogged = isLogged;
    }

    // ======================================
    // =            Constructeurs           =
    // ======================================

    // ======================================
    // =          Methodes publiques        = 
    // ======================================
    public String doSignIn() {
        final String mname = "doSignIn";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            customer = accountService.authenticate(login, password);
            homeAddress = customer.getHomeAddress();
            navigateTo = "customer.signed.in";
            isLogged = true;
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return "main.jsf?faces-redirect=true";
    }

    public String doCreateNewAccount() {
        final String mname = "doCreateNewAccount";
        logger.entering(cname, mname);

        String navigateTo = null;
       
        // Id and password must be filled
        if ("".equals(customer.getLogin()) || "".equals(customer.getPassword()) || "".equals(password2)) {
            addWarningMessage("Id and passwords have to be filled");
            navigateTo = null;

        } else if (!customer.getPassword().equals(password2)) {
            addWarningMessage("Both entered passwords have to be the same");
            navigateTo = null;

        } else {
            navigateTo = "createaccount.jsf?faces-redirect=true";
        }

        logger.exiting(cname, mname, navigateTo);
        return navigateTo;
    }

    public String doGoOnSignIn()
    {
        if (customer == null) customer = new Customer();
        return "signon.jsf?faces-redirect=true";
    }
    
    public String doCreateCustomer() {
        final String mname = "doCreateCustomer";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            // Creates the customer
           
            
            customer = accountService.createCustomer(customer, homeAddress);
            homeAddress = customer.getHomeAddress();
            isLogged = true;
            navigateTo = "main.jsf?faces-redirect=true";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }
        
        logger.exiting(cname, mname, navigateTo);
        return navigateTo;
    }

    public String doUpdateAccount() {
        final String mname = "doUpdateAccount";
        logger.entering(cname, mname);

        String navigateTo = null;

        try {
            // Updates the customer
            customer = accountService.updateCustomer(customer, homeAddress);
            homeAddress = customer.getHomeAddress();
            navigateTo = "updateaccount.jsf?faces-redirect=true";
        } catch (Exception e) {
            addMessage(cname, mname, e);
        }

        logger.exiting(cname, mname, navigateTo);
        return "updateaccount.jsf?faces-redirect=true";
    }

    public String doSignOff() {
        final String mname = "doSignOff";
        logger.entering(cname, mname);

        String navigateTo = "main.jsf?faces-redirect=true";

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        session.invalidate();
        isLogged = false;
        customer = new Customer();
        logger.exiting(cname, mname, navigateTo);
        return navigateTo;
    }

    // ======================================
    // =          Methodes Protégées        =
    // ======================================

    // ======================================
    // =             Accesseurs             =
    // ======================================
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }

    // ======================================
    // =           Methodes Privées         =
    // ======================================

    // ======================================
    // =   Methodes hash, equals, toString  =
    // ======================================
}
